package com.blb.zycommunity.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zycommunity.entity.SysRole;
import com.blb.zycommunity.entity.SysUser;
import com.blb.zycommunity.mapper.SysUserMapper;
import com.blb.zycommunity.mapper.SysUserRoleMapper;
import com.blb.zycommunity.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import java.time.LocalDateTime;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {


    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public IPage<SysUser> findUserPage(Long current, Long size,String deptName,String status, String userName, String phonenumber, LocalDateTime startTime, LocalDateTime finishTime) {
        return sysUserMapper.selectUserPage(new Page<>(current,size),deptName, status,  userName,  phonenumber, startTime, finishTime);
    }

    @Override
    public List<SysUser> findPostByUserId(Long userId) {
        return sysUserMapper.selectPostByUserId(userId);
    }

    @Override
    public List<SysUser> findRoleByUserId(Long userId) {
        return sysUserMapper.selectRoleByUserId(userId);
    }

    @Override
    public boolean updateUserStatus(String status, Long userId) {
        return sysUserMapper.updateUserStatus(status,userId);
    }

    @Override
    public IPage<SysUser> findUserPage(Long current, Long size, String deptName) {
        return null;
    }

    @Override
    public List<GrantedAuthority> getAuthoritiesByUsername(String username) {
        //给用户进行授权
        StringBuilder stringBuilder = new StringBuilder();
        //遍历所有角色，加前缀 ROLE_
        List<SysRole> roles = sysUserRoleMapper.selectRolesByUsername(username);
        roles.forEach(role -> {
            stringBuilder.append("ROLE_" + role.getRoleName() + ",");
        });
        //将字符串转换为权限集合
        List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(stringBuilder.toString());
        return authorities;
    }

    @Override
    public SysUser getUserByUsername(String username) {
        SysUser user = sysUserMapper.selectUserByUsername(username);
        return user;
    }

    @Override
    public List<SysUser> findUserSex() {
        return sysUserMapper.selectUserSex();
    }

    @Override
    public boolean updateUserPassword(String password, Long userId) {
        return sysUserMapper.updateUserPassword(password,userId);
    }

    @Override
    public boolean removeUser(Long userId) {
        return sysUserMapper.deleteUser(userId);
    }

    @Override
    public void batchDeleteUser(List<Long> userIds) {
        sysUserMapper.batchDeleteUser(userIds);
    }

    @Override
    public List<SysUser> findInformationByUsername(String username) {
        return sysUserMapper.selectInformationByUsername(username);
    }

    @Override
    public SysUser excelUserById(Long userId) {
        return sysUserMapper.excelUserById(userId);
    }


}
